import { IVec2 } from "../Interface/State"
import {
    IActorConfig, IAttrConfig, IEnemyConfig, IEntityConfig, ILevelPartConfig, IPropConfig, ISelectPoolConfig,
    ISkillConfig, ITalentConfig, IWeaponConfig
} from "./Config"
import { EntityTypeEnum, EnemySkillTypeEnum, LevelTypeEnum } from "./Enum"

type GJson = {
    [key: number]: {
        sn: number,
        name: string,
        type: 1 | 2 | 3 | 4 | 5,
        param1: any,
        param2: any,
    }
}

/** 房间内玩家最大数量 */
export const GHomeMaxPlayerNum: number = 4

/** 转发用户输入延迟时间/毫秒 */
export const GSyncDelay: number = 100

/** webSocket 地址 */
// export const GWsAddress = 'ws://192.168.130.60'
export const GWsAddress: string = 'ws://localhost'

/** 怪物基础生命值 */
export const GEnemyHP: Record<number, number> = {}

/** 移速 */
export const GMoveSpeed: Record<number, number> = {

    [EntityTypeEnum.bullet1]: 1500,
    [EntityTypeEnum.bullet2]: 1600,
    [EntityTypeEnum.bullet3]: 250,
}

/** 枪口相对坐标 */
export const GWeaponMuzzle: Record<number, IVec2> = {
    [EntityTypeEnum.weapon1]: { x: 10, y: 1.5 },
    [EntityTypeEnum.weapon2]: { x: 13, y: 0.5 },
    [EntityTypeEnum.weapon3]: { x: 8.5, y: 1 },
    [EntityTypeEnum.weapon4]: { x: 11.5, y: 1 },
    [EntityTypeEnum.weapon5]: { x: 7.5, y: 1.7 },
    [EntityTypeEnum.weapon6]: { x: 7.3, y: 1.2 },
    [EntityTypeEnum.weapon7]: { x: 7.3, y: 1.2 },
    [EntityTypeEnum.weapon8]: { x: 7.3, y: 1.2 },
    [EntityTypeEnum.weapon9]: { x: 12, y: 0.7 },
    [EntityTypeEnum.weapon10]: { x: 16.5, y: 0.25 },
}

/** 武器射击半径,包括近战武器 */
export const GWeaponShootRadiu: Record<number, number> = {

    [EnemySkillTypeEnum.enemyGun]: 450,
    [EnemySkillTypeEnum.enemyImpact]: 300,
    [EnemySkillTypeEnum.enemyImpactPlus]: 350,
}

/** 怪物技能cd */
export const GSEnemySKillCD: Record<number, number> = {
    [EnemySkillTypeEnum.enemyGun]: 4,
    [EnemySkillTypeEnum.enemyImpact]: 4,
    [EnemySkillTypeEnum.enemyImpactPlus]: 6,
    [EnemySkillTypeEnum.recoverHpHalo]: 0.5,

}

/** 武器使用的子弹 */
export const GWeaponBullet: Record<number, number> = {
    [EntityTypeEnum.weapon1]: EntityTypeEnum.bullet2,
    [EntityTypeEnum.weapon2]: EntityTypeEnum.bullet1,
    [EntityTypeEnum.weapon3]: EntityTypeEnum.bullet2,
    [EntityTypeEnum.weapon4]: EntityTypeEnum.bullet1,
    [EntityTypeEnum.weapon5]: EntityTypeEnum.bullet2,
    [EntityTypeEnum.weapon6]: EntityTypeEnum.bullet1,
    [EntityTypeEnum.weapon7]: EntityTypeEnum.bullet2,
    [EntityTypeEnum.weapon8]: EntityTypeEnum.bullet1,
    [EntityTypeEnum.weapon9]: EntityTypeEnum.bullet2,
    [EntityTypeEnum.weapon10]: EntityTypeEnum.bullet1,
}

/** 子弹杀伤半径 */
export const GBulletRadius: Record<number, number> = {
    [EntityTypeEnum.bullet1]: 5,
    [EntityTypeEnum.bullet2]: 5,
    [EntityTypeEnum.bullet3]: 40,
}

/** 子弹的存活时间 */
export const GBulletLiveTime: Record<number, number> = {
    [EntityTypeEnum.bullet1]: 0.4,
    [EntityTypeEnum.bullet2]: 0.3,
    [EntityTypeEnum.bullet3]: 3,
}

/** 怪物距离玩家距离，远靠近，近远离 */
export const GEnemyDisPlayer: Record<number, number> = {
    [EntityTypeEnum.enemy_shooter]: 400,
    [EntityTypeEnum.enemy_shooter_plus]: 450,
}

/** boss1的cd */
export const GBoss1CD = {
    shootCD: 1.5,
    impactCD: 3,
    standCastingTime: 0.6,
    duration: 1,
}

/** 冲锋属性 */
export const GEnemyImpact = { speed: 1200, duration: 0.8 }

/** 冲锋属性 */
export const GEnemyImpactPlus = { speed: 1200, duration: 1.6 }

/** 散弹枪属性 */
export const GWeapon1BulletAttr = {
    /** 子弹偏移角度 */
    angle: 8,
    /** 子弹数量 */
    num: 5
}

/** 双管猎枪 */
export const GWeapon3BulletAttr = {
    /** 子弹偏移角度 */
    angle: 3,
    /** 子弹数量 */
    num: 10
}

/** 对应global配置表数据 */
export const G = {

    /** 武器瞄准速度 */
    WeaponAngelSpeed: 4000,

    /** 场景大小 */
    StageSize: { w: 1500, h: 1500 },

    /** 角色和小怪的规格 */
    NormalEntitySize: { w: 64, h: 96 },

    /** 精英的规格 */
    EliteEntitySize: { w: 80, h: 120 },

    /** boss的规格 */
    BossEnemySize: { w: 128, h: 192 },

    /** 子弹击中特效的规格 */
    ExplosionEntitySize: { w: 48, h: 48 },

    /** 子弹的规格 */
    BulletEntitySize: { w: 84, h: 84 },

    /** 吃经验球范围 */
    EatExpRange: 80,

    /** 蓄力时间 */
    CastingTime: 0.6,

    /** 武器搜寻敌人频率 */
    SearchCD: 0.1,

    normalMass: 1,
    /** 精英质量 */
    eliteMass: 5,
    /** boss质量 */
    bossMass: 10,
    // /** 重武器减少的移速 */
    // weaponReduceMoveSpeed: 10
}

/** 初始化全局参数 */
export const initGlobal = (data: GJson) => {
    const tempG = G as { [key: string]: any }
    for (const i in data) {
        const v = data[i]
        if (v.type === 1) {
            tempG[v.name] = v.param1
        } else if (v.type === 2) {
            tempG[v.name] = { w: v.param1, h: v.param2 }
        }
    }
}

type WeaponJson = { [key: string]: IWeaponConfig }
/** 武器配置 */
export const GWeapon: WeaponJson = {}
/** 初始化武器配置 */
export const initWeapon = (data: WeaponJson) => {
    for (const i in data) {
        GWeapon[i] = data[i]
    }
}

type EnemyJson = { [key: number]: IEnemyConfig }
/** 怪物配置 */
export const GEnemy: EnemyJson = {}
/** 初始化怪物配置 */
export const initEnemy = (data: EnemyJson) => {
    for (const i in data) {
        const wData = data[i]
        GEnemy[i] = wData
        GMoveSpeed[i] = wData.speed
        GEnemyHP[i] = wData.hp
    }
}

export type ActorJson = { [key: number]: IActorConfig }
/** 初始化角色配置 */
export const initActor = (data: ActorJson) => {
    for (const i in data) {
        const wData = data[i]
        GMoveSpeed[i] = wData.speed
    }
}

/** 难度 */
type ChapterJson = {
    /** 关卡 */
    [key: string]: {
        /** 怪物批次 */
        [key: number]: {
            /** 关卡战斗时间 */
            duration: number,
            /** 怪物批次id */
            parts?: number[],
            /** 生命值比例 */
            hpRatio: number,
            /** boss数组 */
            bossArr?: number[],
            /** 关卡类型 */
            levelType: LevelTypeEnum,
        }
    }
}

/** 章节信息 */
export const GChapter: ChapterJson = {}
/** 初始化章节信息 */
export const initChapter = (data: ChapterJson[keyof ChapterJson], chapter: number) => {
    for (const i in data) {
        const v = data[i]
        if (!GChapter[chapter]) {
            GChapter[chapter] = {}
        }
        GChapter[chapter][Number(i)] = v
    }
}


/** 关卡内怪物批次 */
type LevelPartJson = { [key: number]: ILevelPartConfig }
/** 关卡出怪批次信息 */
export const GLevelPart: LevelPartJson = {}
/** 初始化关卡怪物批次信息 */
export const initLevelPart = (data: LevelPartJson) => {
    for (const i in data) {
        GLevelPart[i] = data[i]
    }
}


/** 道具 */
type PropJson = { [key: string]: IPropConfig }
/** 道具配置 */
export const GProp: PropJson = {}
/** 初始化道具配置 */
export const initProp = (data: PropJson) => {
    for (const i in data) {
        GProp[i] = data[i]
    }
}

/** 角色经验 */
type expJson = {
    [key: number]: {
        sn: number,
        /** 等级 */
        level: number,
        /** 升级所需经验 */
        exp: number,
    }
}
/** 角色经验配置 */
export const GExp: expJson = {}
/** 初始化道具配置 */
export const initExp = (data: expJson) => {
    for (const i in data) {
        GExp[i] = data[i]
    }
}

/** 选择池 */
type SelectPoolJson = { [key: number]: ISelectPoolConfig }
export const GSelectPool: SelectPoolJson = {}
export const initSelectPool = (data: SelectPoolJson) => {
    for (const i in data) {
        GSelectPool[i] = data[i]
    }
}

/** 实体对象 */
type EntityJson = { [key: number]: IEntityConfig }
export const GEntity: EntityJson = {}
export const initEntity = (data: EntityJson) => {
    for (const i in data) {
        GEntity[i] = data[i]
    }
}

/** 属性 */
type AttributeJson = { [key: number]: IAttrConfig }
export const GAttribute: AttributeJson = {}
export const initAttribute = (data: AttributeJson) => {
    for (const i in data) {
        GAttribute[i] = data[i]
    }
}

/** 技能 */
type SkillJson = { [key: number]: ISkillConfig }
export const GSkill: SkillJson = {}
export const initSkill = (data: SkillJson) => {
    for (const i in data) {
        GSkill[i] = data[i]
    }
}

/** 技能 */
type TalentJson = { [key: number]: ITalentConfig }
export const GTalent: TalentJson = {}
export const initTalent = (data: TalentJson) => {
    for (const i in data) {
        GTalent[i] = data[i]
    }
}